<template>
  <div style="display: flex" ref="imageWrapper">
    <div style="flex: 1; margin-right: 15px; max-width: 250px">
      <!-- :id="id"
       :isload="isload"
       @getTree="hadleGetTreeList" -->
      <LeftTree title="科目" ref="leftTree" @selected="getTreeData"></LeftTree>
    </div>
    <div style="flex: 5; overflow: auto">
      <a-card title="查询条件" class="search" style="margin: 0px 0px">
        <!-- 查询区域 -->
        <div class="table-page-search-wrapper">
          <a-form layout="inline" @keyup.enter.native="searchQuery">
            <a-row :gutter="24">
              <!-- <a-col v-if="isShowAddctionParams && this.listData.length > 0" :md="6" :sm="8">
                <a-form-item label="机构名称">
                  <a-input v-model="orgName" readOnly placeholder="请选择" @click.native="openOrganModel" />
                </a-form-item>
              </a-col> -->
              <a-col v-for="(item, index) in listData" :key="index" :md="6" :sm="8">
                <a-form-item :label="item.fieldCnName">
                  <div v-if="item.queryType === 'queryLike'">
                    <a-input v-model="item.queryValueStart" allowClear></a-input>
                  </div>
                  <div v-else-if="item.queryType === 'queryDate'">
                    <a-range-picker
                      v-model="item.dateArray"
                      style="width: 100%"
                      @change="
                        (value, dateString) => {
                          handlePanelChange(value, dateString, index)
                        }
                      "
                    />
                  </div>
                  <div v-else-if="item.queryType === 'queryCompare'">
                    <a-select allowClear style="width: 45%" placeholder="请选择" v-model="item.queryValueStart">
                      <a-select-option value=">">大于</a-select-option>
                      <a-select-option value="<">小于</a-select-option>
                      <a-select-option value="=">等于</a-select-option>
                      <a-select-option value=">=">大于等于</a-select-option>
                      <a-select-option value="<=">小于等于</a-select-option>
                    </a-select>
                    <a-input-number
                      allowClear
                      style="margin-left: 5%; width: 50%"
                      v-model="item.queryValueEnd"
                    ></a-input-number>
                  </div>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="8">
                <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
                  <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
                  <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px"
                    >清除条件</a-button
                  >
                  <a-button v-if="isBackButton" type="primary" style="margin-left: 8px" @click="backUppage">
                    返回
                  </a-button>
                </span>
              </a-col>
            </a-row>
          </a-form>
        </div>
      </a-card>
      <a-card style="margin: 20px 0px 0px 0px" :bordered="false">
        <a-table
          ref="table"
          size="middle"
          rowKey="id"
          class="antableClass"
          :columns="columns"
          :pagination="ipagination"
          :dataSource="dataSource"
          :loading="loading"
          @change="handleTableChange"
          :scroll="{ x: true }"
        >
          <template v-for="i in columns.slice(1)" :slot="i.dataIndex" slot-scope="text">
            <div :key="i.dataIndex" >
              <j-ellipsis v-if="text&&text.length > 20" :value="text" :length="20" />
              <span v-else>{{ text }}</span>
            </div>
          </template>
        </a-table>
      </a-card>
    </div>
    <CheckOrganModal ref="departModal" @ok="updateQueryParam" />
  </div>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import columns from './indexColumns'
import LeftTree from './LeftTree'
import { httpAction, getAction, postAction } from '@/api/manage'
import CheckOrganModal from '@/views/sj/sjxmzygl/yzzx/testcomponent2/CheckOrganModal'
import moment from 'moment'
import 'moment/locale/zh-cn'
import _ from 'lodash'
import jumpConfigMixin from '@/views/sj/sjxmzygl/sjxmss/jumpConfigMixin'

export default {
  props: ['id', 'type'],
  name: 'mxsjzs',
  components: {
    LeftTree,
    CheckOrganModal,
  },
  mixins: [JeecgListMixin, jumpConfigMixin],
  data() {
    return {
      queryParam: {},
      superParam: {
        modelId: '',
      },
      listData: [],
      selectedData: '',
      radioValue: '',
      columns: columns(this),
      url: {
        list: '/dataModel/findResultList',
        getSearchAndTitle: '/dataModel/queryTitleAndSearch',
      },
      current: 0,
      dateArray: [],
      getListData: [],
      isBackButton: false,
      disableMixinCreated: true,
      isShowAddctionParams: true,
      Addction: {
        queryType: 'queryCompare',
        queryKey: 'finId',
        fieldCnName: '机构名称',
        queryValueStart: '=',
        queryValueEnd: '',
        dateArray: [],
      },
      orgName: '',
    }
  },
  watch: {
    $route: {
      handler: function (to) {
        if (Object.keys(to.params).length && to.params.from == 'sjxmss') {
          let { returnRecord } = to.params
          this.isBackButton = true
          this.returnRecord = returnRecord
        }
      },
      immediate: true,
    },
  },
  methods: {
    searchReset() {
      this.dataSource = []
      this.dateArray = []
      this.Addction = this.$options.data().Addction
      this.orgName = ''
      this.listData = _.cloneDeep(this.getListData)
      this.queryParam = this.$options.data().queryParam
      this.ipagination = this.$options.data().ipagination
    },
    onChangeSteps(current) {
      console.log('onChangeSteps:', current)
      // this.current = current;
    },
    updateQueryParam(data) {
      console.log(data, 'daat')
      //  queryValueStart 存id  queryValueEnd 存name
      this.orgName = data.orgName
      this.Addction.queryValueEnd = data.orgId
    },
    openOrganModel() {
      this.$refs.departModal.edit()
    },
    async getTreeData(data, JumpRecord = '', isShowAddctionParams = true) {
      this.superParam.modelId = data
      this.isShowAddctionParams = isShowAddctionParams
      await this.getSearchAndTitleList(data)
      if (JumpRecord != '') {
        let jumprecord = JSON.parse(JumpRecord)
        let flag = true
        this.listData.map((item) => {
          if (item.queryKey == 'entpSubEntpNm' || item.queryKey == 'subEntpNm' || item.queryKey == 'orgNm') {
            item.queryValueStart = jumprecord.sub_entp_nm
          } else if (item.queryKey == 'conNm') {
            item.queryValueStart = jumprecord.con_nm
          } else if (item.queryKey == 'dvceNm') {
            item.queryValueStart = jumprecord.dvce_nm
          } else if (item.queryKey == 'matClsNm') {
            item.queryValueStart = jumprecord.mat_cls_nm
          } else if (item.queryKey == 'chkinOdrNum') {
            item.queryValueStart = jumprecord.chkin_odr_num
          } else if (item.queryType == 'queryDate' && flag) {
            item.queryValueStart = jumprecord.startDate
            item.queryValueEnd = jumprecord.endDate
            item.dateArray = [jumprecord.startDate, jumprecord.endDate]
            flag = false
          } else if (item.queryKey == 'signDt') {
            item.queryValueStart = jumprecord.startDate
          }
        })
      }

      //查询
      this.searchQuery()
    },
    getSearchAndTitleList(id) {
      debugger
      this.listData = []
      this.Addction = this.$options.data().Addction
      this.orgName = ''
      this.dataSource = []
      this.dateArray = []
      this.columns = columns(this)
    console.log(this.columns);
      return getAction(this.url.getSearchAndTitle, { id: id }).then((res) => {
        this.loading = false
        if (res.success) {
          res.result.searchList.map((item, index) => {
            this.listData.push({
              queryType: item.searchType,
              queryKey: item.fieldEnName,
              fieldCnName: item.fieldCnName,
              queryValueStart: '',
              queryValueEnd: '',
              dateArray: [],
            })

            this.getListData = _.cloneDeep(this.listData)
          })
          res.result.titleList.map((item, index) => {
            this.columns.push({
              title: item.fieldCnName,
              align: 'center',
              dataIndex: item.fieldEnName,
              scopedSlots: { customRender: item.fieldEnName },
            })
          })
        } else {
          this.$message.warning(res.message)
        }
      })
    },
    loadData(arg) {
      if (!this.url.list) {
        this.$message.error('请设置url.list属性!')
        return
      }
      console.log('columns', this.columns)
      //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1
      }
      if (this.isShowAddctionParams && this.listData.length > 0)
        this.queryParam.listData = JSON.stringify([this.Addction, ...this.listData])
      var params = this.getQueryParams() //查询条件
      this.loading = true
      postAction(this.url.list, params).then((res) => {
        if (res.success) {
          //update-begin---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
          this.dataSource = res.result.records || res.result
          if (res.result.total) {
            this.ipagination.total = res.result.total
          } else {
            this.ipagination.total = 0
          }
          //update-end---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
        }
        if (res.code === 510) {
          this.$message.warning(res.message)
        }
        this.loading = false
      })
    },
    moment,
    // 得到年份选择器的值
    handlePanelChange(value, dateString, index) {
      if (value.length) {
        this.listData[index].queryValueStart = moment(value[0]).format('YYYY-MM-DD')
        this.listData[index].queryValueEnd = moment(value[1]).format('YYYY-MM-DD')
      } else {
        this.listData[index].queryValueStart = ''
        this.listData[index].queryValueEnd = ''
      }
    },
  },
}
</script>
<style lang="less" scoped>
@import '~@assets/less/tableTheme.less';
@import '~@assets/less/common.less';
/deep/.ant-table td {
  white-space: nowrap;
}
</style>